package database;

import sharedspace.*;

import java.sql.SQLException;
import java.util.*;
/**
 * Responsible for data transition from SQL server to the server
 * @author G3
 *
 */
public class QuestionControl {
	
	private Question question = null;
	private QuestionInTest questionInTest = null;
	private List<Question> questionList = null;
	private List<QuestionInTest> questionInTestList = null;
	private Question question1;
	
	
	
	public QuestionControl()
	{
		
	}
	/**
	 * Generate an array list with all questions
	 * @return ArrayList<Question> list
	 */	
	public List<Question> getQuestionList()
	{
		int questionSize = JDBC.countStatment("SELECT count(*) FROM question q;");
		questionList = new ArrayList<Question>();
		Object[][] result = new Object[questionSize][Question.NUM_OF_COLUMNS];
		JDBC.selectStatmentMatrix("SELECT * FROM question q", result, Question.NUM_OF_COLUMNS, questionSize);
		for(int i = 0; i<questionSize; i++)
		{
			question = new Question();
			setQuestion(result, i);
			questionList.add(question);
		}
		
		return questionList;
	}
	/**
	 * Generates a list of questions written by a specific author<br>
	 * if isInUse = 0 picks only questions that are not being used<br> 
	 * if isInUse = 1 picks all questions
	 * @param author
	 * @param isInUse
	 * @return ArrayList<Question> questionList
	 */
	public List<Question> getQuestionListByAuthor(String author, boolean isInUse)
	{
		int questionSize;
		if (isInUse)
			questionSize = JDBC.countStatment("SELECT count(*) FROM question q WHERE q.author=" + author + ";");
		else 
			questionSize = JDBC.countStatment("SELECT count(*) FROM question q WHERE q.author=" + author + " AND q.inUse=0;");

		questionList = new ArrayList<Question>();
		Object[][] result = new Object[questionSize][Question.NUM_OF_COLUMNS];
		if (isInUse)
			JDBC.selectStatmentMatrix("SELECT * FROM question q WHERE q.author=" + author + ";", result, Question.NUM_OF_COLUMNS, questionSize);
		else
			JDBC.selectStatmentMatrix("SELECT * FROM question q WHERE q.author=" + author + " AND q.inUse=0;", result, Question.NUM_OF_COLUMNS, questionSize);
		
		for(int i = 0; i<questionSize; i++)
		{
			question = new Question();
			setQuestion(result, i);
			questionList.add(question);
		}
		
		return questionList;
	}
	/**
	 * Generates a list of questions which belongs to a specific subject
	 * @param subjectID
	 * @return ArrayList<Question> questionList
	 */
	public List<Question> getQuestionListBySubjectID(String subjectID)
	{
		int questionSize = JDBC.countStatment("SELECT count(*) " +
											  "FROM question q " +
											  "Where q.questionSubject=" + subjectID + ";");
		questionList = new ArrayList<Question>();
		Object[][] result = new Object[questionSize][Question.NUM_OF_COLUMNS];
		JDBC.selectStatmentMatrix("SELECT * " +
								  "FROM question q " +
								  "Where q.questionSubject='" + subjectID + "';", result, Question.NUM_OF_COLUMNS, questionSize);
		for (int i = 0; i<questionSize; i++)
		{
			question = new Question();
			setQuestion(result, i);
			questionList.add(question);
		}
		return questionList;
	}
	/**
	 * Generates a list of questions which belongs to a specific test
	 * @param testID
	 * @return ArrayList<Question> questionList
	 */
	public List<QuestionInTest> getQuestionListByTestID(String testID)
	{
		int questionSize = JDBC.countStatment("SELECT count(*) " +
											  "FROM questionintest qit " +
											  "WHERE qit.testID='" + testID + "';");
		
		questionInTestList = new ArrayList<QuestionInTest>();
		Object[][] result = new Object[questionSize][QuestionInTest.NUM_OF_COLUMNS];
		JDBC.selectStatmentMatrix("SELECT * " +
				  				  "FROM questionintest qit " +
				  				  "WHERE qit.testID='" + testID + "';", result, QuestionInTest.NUM_OF_COLUMNS, questionSize);
		for(int i = 0; i<questionSize; i++)
		{
			questionInTest = new QuestionInTest();
			setQuestionInTest(result, i);
			questionInTestList.add(questionInTest);
		}		
		return questionInTestList;
		
	}
	
	/**
	 * Returns an entire question instance by a specific question ID
	 * @param questionID
	 * @return Question question
	 */
	public Question getQuestionByQuestionID(String questionID)
	{
		question = new Question();
		Object[] result = new Object[Question.NUM_OF_COLUMNS];
		JDBC.selectStatment("SELECT * FROM question q WHERE q.questionID=" + questionID + ";", result, Question.NUM_OF_COLUMNS);
		setQuestion(result);
		return question;
	}
	
	/**
	 * Create new question and set its values according to the data in row in matrix[row][column] result
	 * @param result
	 * @param row
	 */
	public void setQuestion(Object[][] result, int row)
	{
		question.setQuestionID(result[row][0].toString());
		question.setQuestionSubject(result[row][1].toString());
		question.setQuestion(result[row][2].toString());
		String[] answers = new String[4];
		for (int i = 0; i<4; i++)
			answers[i] = result[row][i+3].toString();
		question.setAnswers(answers);
		question.setCorrectAnswer(Integer.parseInt(result[row][7].toString()));
		//question.setCorrectAnswer(Integer.parseInt(result[row][8].toString()));
		question.setAuthor((new TeacherControl().getTeacherByTeacherID(result[row][8].toString())));
		question.setInUse(Boolean.parseBoolean(result[row][9].toString()));
		question.setQuestionCommet(result[row][10].toString());
		
	}
	/**
	 * Create new question and set its values according to the data  result[column]
	 * @param result
	 */
	public void setQuestion(Object[] result)
	{
		question.setQuestionID(result[0].toString());
		question.setQuestionSubject(result[1].toString());
		question.setQuestion(result[2].toString());
		String[] answers = new String[4];
		for (int i = 0; i<4; i++)
			answers[i] = result[i+3].toString();
		question.setAnswers(answers);
		question.setCorrectAnswer(Integer.parseInt(result[7].toString()));
		//question.setCorrectAnswer(Integer.parseInt(result[8].toString()));
		question.setAuthor((new TeacherControl().getTeacherByTeacherID(result[8].toString())));
		question.setInUse(Boolean.parseBoolean(result[9].toString()));
		question.setQuestionCommet(result[10].toString());
		
	}
	
	public void setQuestionInTest(Object[][] result, int row)
	{
		questionInTest.setQuestion(getQuestionByQuestionID(result[row][0].toString()));
		questionInTest.setTest((new TestControl()).getTestByTestID(result[row][1].toString()));
		questionInTest.setPointQuestion(Integer.parseInt(result[row][2].toString()));
	}
	
	/**
	 * Updates a question to the DB
	 * @param q
	 */
	public void updateQuestion(Question q)
	{
		JDBC.updateStatment("question", "question", "'"+q.getQuestion()+"'", "questionID", q.getQuestionID());
		JDBC.updateStatment("question", "answer1", "'"+q.getAnswers()[0]+"'", "questionID", q.getQuestionID());
		JDBC.updateStatment("question", "answer2", "'"+q.getAnswers()[1]+"'", "questionID", q.getQuestionID());
		JDBC.updateStatment("question", "answer3", "'"+q.getAnswers()[2]+"'", "questionID", q.getQuestionID());
		JDBC.updateStatment("question", "answer4", "'"+q.getAnswers()[3]+"'", "questionID", q.getQuestionID());
		JDBC.updateStatment("question", "correctAnswer", ""+q.getCorrectAnswer(), "questionID",q.getQuestionID());
		JDBC.updateStatment("question", "questionComment", "'"+q.getQuestionCommet()+"'", "questionID",q.getQuestionID());
	}
	/**
	 * Inserts a new question to the DB
	 * @param qInTest
	 */
	public void insertQuestionInTest(QuestionInTest qInTest){
		
		
		JDBC.insertStatment("INSERT into questionintest "+"value('"+qInTest.getQuestion().getQuestionID()+"','"+qInTest.getTest().getTestID()+"','"+qInTest.getPointQuestion()+"');");
		
	}
	
	public void updateInUseQuestoin(QuestionInTest qIntest)
	{
	JDBC.insertStatment("UPDATE question SET question.inUse=1 WHERE aes.question.questionID="+qIntest.getQuestion().getQuestionID());
	}
}// end QuestionControl
